﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;
namespace BPM.Foundation.PWService
{
    public partial interface IPowerService
    {
    }

    public partial class PowerService : IPowerService
    {
        //[5.1	将人员添加到部门]
        public int? addUserInDepartment(string pid, string userNo, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(userNo) || string.IsNullOrEmpty(departmentNo))
            {
                return 1;
            }
            //-

            //[2]
            if (PWDB.PW_User.Count(p => p.userNo == userNo) != 1)
            {
                return 2;
            }
            //-

            //[3]
            if (PWDB.PW_Department.Count(p => p.departmentNo == departmentNo) != 1)
            {
                return 3;
            }
            //-

            //[4]
            if (PWDB.PW_DutyInDepartment.Count(p => p.departmentNo == departmentNo && p.dutyNo == "officeuser") == 0)
            {
                return 4;
            }
            //-

            //[5]
            if (PWDB.PW_DepartmentDutyUserRelation.Count(p => p.departmentNo == departmentNo && p.userNo == userNo && p.dutyNo == "officeuser") == 0)
            {
                PW_DepartmentDutyUserRelation pw_DepartmentDutyUserRelation = new PW_DepartmentDutyUserRelation();
                pw_DepartmentDutyUserRelation.ID = Guid.NewGuid();
                pw_DepartmentDutyUserRelation.departmentNo = departmentNo;
                pw_DepartmentDutyUserRelation.dutyNo = "officeuser";
                pw_DepartmentDutyUserRelation.userNo = userNo;
                PWDB.PW_DepartmentDutyUserRelation.InsertOnSubmit(pw_DepartmentDutyUserRelation);
                return submit();
            }
            else
            {
                return 5;
            }
            //-
        }
        //-

        //[5.2	将人员移除部门]
        public int? deleteUserFromDepartment(string pid, string userNo, string departmentNo)
        {
            if (checkKey(userNo)) { return -1000; }


            //[1]
            if (string.IsNullOrEmpty(userNo) || string.IsNullOrEmpty(departmentNo))
            {
                return 1;
            }
            //-

            //[2]
            if (PWDB.PW_DepartmentDutyUserRelation.Count(p => p.userNo == userNo && p.departmentNo == departmentNo) > 0)
            {
                var v = PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo && p.departmentNo == departmentNo).ToList();
                foreach (var x in v)
                {
                    PWDB.PW_DepartmentDutyUserRelation.DeleteOnSubmit(x);
                }
                return submit();
            }
            else
            {
                return 2;
            }
            //-
        }
        //-

        //[5.3	得到没分配部门的人员所属]
        public List<PW_User> getUserNoDepartment(string pid)
        {
            //[1]
            List<PW_User> pw_Users = PWDB.PW_User.Where(p => true).ToList();
            List<PW_User> pw_UsersInDepartMent = (from temp1 in PWDB.PW_User
                                                  join temp2 in PWDB.PW_DepartmentDutyUserRelation
                                                  on temp1.userNo equals temp2.userNo
                                                  select (temp1)).ToList();
            var pw_UsersNoDepartment = pw_Users.Except(pw_UsersInDepartMent).ToList();
            return pw_UsersNoDepartment;
            //-
        }
        //-

        //[5.4	得到人员所属部门]
        public List<PW_Department> getUserDepartment(string pid, string userNo)
        {
            //[1]
            if (string.IsNullOrEmpty(userNo))
            {
                return null;
            }
            //-

            //[2]

            var vResult = PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo).Select(p => p.departmentNo);
            if (vResult == null)
            {
                return null;
            }

            List<PW_Department> myList = new List<PW_Department>();
            vResult = vResult.Distinct();
            foreach (var obj in vResult)
            {
                var dp = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == obj);
                if (dp != null)
                {
                    myList.Add(dp);
                }
            }



            return myList;
            //-
        }
        //-

        //[5.5	得到部门所属人员]
        public List<PW_User> getDepartmentUser(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]

            var vResult = PWDB.PW_DepartmentDutyUserRelation.Where(p => p.departmentNo == departmentNo).Select(p => p.userNo);
            if (vResult == null)
            {
                return null;
            }

            if (vResult.ToList().Count == 0)
            {
                return null;
            }

            List<PW_User> list = new List<PW_User>();
            vResult = vResult.Distinct();
            foreach (var obj in vResult)
            {
                var user = PWDB.PW_User.SingleOrDefault(p => p.userNo == obj);
                if (user != null)
                {
                    list.Add(user);
                }
            }


            return list;
            //-
        }
        //-

        //[5.6	设定职能与部门的关系]
        public int? bindDutyDepartment(string pid, string dutyNo, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(dutyNo) || string.IsNullOrEmpty(departmentNo))
            {
                return 1;
            }
            //-

            //[2]
            if (PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo) != 1)
            {
                return 2;
            }
            //-

            //[3]
            if (PWDB.PW_Department.Count(p => p.departmentNo == departmentNo) != 1)
            {
                return 3;
            }
            //-

            //[4]
            if (PWDB.PW_DutyInDepartment.Count(p => p.departmentNo == departmentNo && p.dutyNo == dutyNo) == 0)
            {
                PW_DutyInDepartment pw_DutyInDepartment = new PW_DutyInDepartment();


                pw_DutyInDepartment.description = "用户添加";
                pw_DutyInDepartment.ID = Guid.NewGuid();
                pw_DutyInDepartment.departmentNo = departmentNo;
                pw_DutyInDepartment.dutyNo = dutyNo;
                PWDB.PW_DutyInDepartment.InsertOnSubmit(pw_DutyInDepartment);


                return submit();
            }
            else
            {
                return 4;
            }
            //-            
        }
        //-

        //[5.7	解除职能与部门的关系]
        public int? unbindDutyDepartment(string pid, string dutyNo, string departmentNo)
        {
            if (checkKey(dutyNo)) { return -1000; }

            //[1]
            if (string.IsNullOrEmpty(dutyNo) || string.IsNullOrEmpty(departmentNo))
            {
                return 1;
            }
            //-

            //[2]
            if (dutyNo == "officeuser")
            {
                return 2;
            }
            //-

            //[3]
            if (PWDB.PW_DutyInDepartment.Count(p => p.dutyNo == dutyNo && p.departmentNo == departmentNo) == 1)
            {
                var v = PWDB.PW_DutyInDepartment.SingleOrDefault(p => p.dutyNo == dutyNo && p.departmentNo == departmentNo);
                PWDB.PW_DutyInDepartment.DeleteOnSubmit(v);
                return submit();
            }
            else
            {
                return 3;
            }
            //-
        }
        //-

        //[5.8	得到部门所拥有的职能]
        public List<PW_Duty> getDutyFromDepartment(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            var vResult = PWDB.PW_DutyInDepartment.Where(p => p.departmentNo == departmentNo).Select(p => p.dutyNo);
            if (vResult == null)
            {
                return null;
            }

            List<PW_Duty> listResult = new List<PW_Duty>();
            vResult = vResult.Distinct();

            foreach (var obj in vResult)
            {
                var dt = PWDB.PW_Duty.SingleOrDefault(p => p.dutyNo == obj);
                if (dt != null)
                {
                    listResult.Add(dt);
                }
            }

            string strOffice = "officeuser";

            PW_Duty office = PWDB.PW_Duty.SingleOrDefault(p => p.dutyNo == strOffice);


            if (office != null)
            {

                if (!listResult.Exists(p => p.dutyNo == strOffice))
                {
                    listResult.Add(office);
                }

            }
            return listResult;
            //-

        }
        //-

        //[5.9	得到职能所属的部门]
        public List<PW_Department> getDepartmentFromDuty(string pid, string dutyNo)
        {
            //[1]
            if (string.IsNullOrEmpty(dutyNo))
            {
                return null;
            }
            //-

            //[2]
            var vResult = PWDB.PW_DutyInDepartment.Where(p => p.dutyNo == dutyNo).Select(p => p.departmentNo);

            if (vResult == null)
            {
                return null;
            }



            vResult = vResult.Distinct();

            List<PW_Department> list = new List<PW_Department>();

            foreach (var obj in vResult)
            {
                var dp = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == obj);
                if (dp != null)
                {
                    list.Add(dp);
                }
            }


            return list;
            //-
        }
        //-

        //[5.10	设定人员与职能的关系]
        public int? bindDutyUser(string pid, string userNo, string dutyNo, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(dutyNo) || string.IsNullOrEmpty(departmentNo) || string.IsNullOrEmpty(userNo))
            {
                return 1;
            }
            //-
            //[2]
            if (PWDB.PW_User.Count(p => p.userNo == userNo) != 1)
            {
                return 2;
            }
            //-
            //[3]
            if (PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo) != 1)
            {
                return 3;
            }
            //-

            //[4]
            if (PWDB.PW_Department.Count(p => p.departmentNo == departmentNo) != 1)
            {
                return 4;
            }
            //-

            //[5]
            if (PWDB.PW_DutyInDepartment.Count(p => p.departmentNo == departmentNo && p.dutyNo == dutyNo) != 1)
            {
                return 5;
            }
            //-

            //[6]
            if (PWDB.PW_DepartmentDutyUserRelation.Count(p => p.departmentNo == departmentNo && p.dutyNo == "officeuser" && p.userNo == userNo) != 1)
            {
                return 6;
            }
            //-

            //[7]
            if (PWDB.PW_DepartmentDutyUserRelation.Count(p => p.departmentNo == departmentNo && p.dutyNo == dutyNo && p.userNo == userNo) == 0)
            {
                PW_DepartmentDutyUserRelation pw_DepartmentDutyUserRelation = new PW_DepartmentDutyUserRelation();
                pw_DepartmentDutyUserRelation.ID = Guid.NewGuid();
                pw_DepartmentDutyUserRelation.departmentNo = departmentNo;
                pw_DepartmentDutyUserRelation.dutyNo = dutyNo;
                pw_DepartmentDutyUserRelation.userNo = userNo;
                PWDB.PW_DepartmentDutyUserRelation.InsertOnSubmit(pw_DepartmentDutyUserRelation);
                return submit();
            }
            else
            {
                return 7;
            }
            //-  
        }
        //-

        //[5.11	解除人员与职能的关系]
        public int? unbindDutyUser(string pid, string userNo, string dutyNo, string departmentNo)
        {
            if (checkKey(userNo)) { return -1000; }

            //[1]
            if (string.IsNullOrEmpty(userNo) || string.IsNullOrEmpty(departmentNo) || string.IsNullOrEmpty(dutyNo))
            {
                return 1;
            }
            //-

            //[2]
            if (dutyNo == "officeuser")
            {
                return 2;
            }
            //-

            //[3]
            if (PWDB.PW_DepartmentDutyUserRelation.Count(p => p.userNo == userNo && p.departmentNo == departmentNo && p.dutyNo == dutyNo) == 1)
            {
                var x = PWDB.PW_DepartmentDutyUserRelation.SingleOrDefault(p => p.userNo == userNo && p.departmentNo == departmentNo && p.dutyNo == dutyNo);
                PWDB.PW_DepartmentDutyUserRelation.DeleteOnSubmit(x);
                return submit();
            }
            else
            {
                return 3;
            }
            //-
        }
        //-

        //[5.12	得到部门所拥有的职能]
        public List<PW_Duty> getDutyFromUser(string pid, string userNo)
        {
            //[1]
            List<PW_Duty> dutysFromUser = (from temp1 in PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo).Distinct()
                                           join temp2 in PWDB.PW_Duty
                                           on temp1.dutyNo equals temp2.dutyNo
                                           select (temp2)).ToList();
            return dutysFromUser;
            //-
        }
        //-

        //[5.13	得到部门树]
        public DepartmentTree getDepartmentTree(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            PW_Department vCurrent = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == departmentNo);
            if (vCurrent == null)
            {
                return null;
            }
            //-

            //[3]
            PW_Department vParent = this.getParentDepartment(pid, departmentNo);


            DepartmentTree dTree = new DepartmentTree();
            dTree.current = vCurrent;
            dTree.parent = vParent;
            dTree.childList = new List<DepartmentTree>();

            addDepartmentTree(dTree, pid);

            return dTree;

        }
        //-

        //[5.14	得到上级部门]
        public PW_Department getParentDepartment(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            var vResult = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == departmentNo);
            if (vResult == null)
            {
                return null;
            }

            return PWDB.PW_Department.SingleOrDefault(p1 => p1.departmentNo == vResult.parentDepartment);
            //-
        }
        //-

        //[5.15	得到下级部门列表]
        public List<PW_Department> getChildDepartment(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            var vResult = PWDB.PW_Department.Where(p => p.parentDepartment == departmentNo && p.parentDepartment != p.departmentNo);
            if (vResult == null)
            {
                return null;
            }
            if (vResult.ToList().Count == 0)
            {
                return null;
            }
            //-
            return vResult.ToList();

        }
        //-

        //[5.16	得到下级所有部门列表]
        public List<PW_Department> getAllChildDepartment(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            var vResult = PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == departmentNo);
            if (vResult == null)
            {
                return null;
            }


            List<PW_Department> list = this.getChildDepartment(pid, departmentNo);

            List<PW_Department> list2 = this.getChildDepartment(pid, departmentNo);
            foreach (var obj in list)
            {
                if (obj.parentDepartment != obj.departmentNo)
                {
                    addChildDepartment(obj, list2, pid);
                }
            }

            return list2;

            //-
        }
        //-

        public List<PW_MenuDutyRelation> getMenuDuty(string pid, string menuNo)
        {
            if (string.IsNullOrEmpty(menuNo))
            {
                return null;
            }
            return PWDB.PW_MenuDutyRelation.Where(a => a.PW_Menu.menuNo == menuNo).ToList();
        }

        public int? insertWfStateRelation(PW_WfStateUserRelation nodeRelation)
        {
            if (string.IsNullOrEmpty(nodeRelation.dutyNo) && string.IsNullOrEmpty(nodeRelation.userNo))
            {
                return 1;
            }
            PWDB.PW_WfStateUserRelation.InsertOnSubmit(nodeRelation);
            return submit();
        }

        public int? updateWfStateRelation(PW_WfStateUserRelation nodeRelation)
        {
            if (string.IsNullOrEmpty(nodeRelation.dutyNo) && string.IsNullOrEmpty(nodeRelation.userNo))
            {
                return 1;
            }
            var p=PWDB.PW_WfStateUserRelation.SingleOrDefault(a=>a.ID==nodeRelation.ID);
            if(p==null)
            { 
                return 2;
            }
            p.templateID=nodeRelation.templateID;
            p.stateID=nodeRelation.stateID;
            p.dutyNo=nodeRelation.dutyNo;
            p.userNo=nodeRelation.userNo;
            p.objectCode=nodeRelation.objectCode;
            p.privilege=nodeRelation.privilege;
            return submit();
        }

        public int? deleteWfStateRelation(PW_WfStateUserRelation nodeRelation)
        {
            if (nodeRelation == null)
                return 1;
            var p = PWDB.PW_WfStateUserRelation.SingleOrDefault(a => a.ID == nodeRelation.ID);
            if (p == null)
            {
                return 2;
            }
            PWDB.PW_WfStateUserRelation.DeleteOnSubmit(p);
            return submit();
        }

        public List<PW_WfStateUserRelation> getWfStateRelation(string templateID, string stateID)
        {
            if (string.IsNullOrEmpty(templateID) || string.IsNullOrEmpty(stateID))
                return null;
            return PWDB.PW_WfStateUserRelation.Where(a => (a.templateID == templateID && a.stateID == stateID)).ToList();
        }

        public List<PW_WfStateUserRelation> getWfStateUser(string templateID, string stateID, string userNo)
        {
            if (string.IsNullOrEmpty(templateID) || string.IsNullOrEmpty(stateID) || string.IsNullOrEmpty(userNo))
                return null;
            var user = PWDB.PW_User.Single(a => a.userNo == userNo);
            if (user == null)
                return null;
            List<PW_WfStateUserRelation> sur = new List<PW_WfStateUserRelation>();
            foreach (var r in user.PW_DepartmentDutyUserRelation)
            {
                var t = PWDB.PW_WfStateUserRelation.Where(a => (a.templateID == templateID && a.stateID == stateID && a.dutyNo == r.dutyNo)).ToList();
                sur.AddRange(t);
            }
            var t2 = PWDB.PW_WfStateUserRelation.Where(a => (a.templateID == templateID && a.stateID == stateID && a.userNo == userNo)).ToList();
            sur.AddRange(t2);
            return sur.Distinct().ToList();
        }

        public List<PW_DepartmentDutyUserRelation> getSuperiorDeptDutyFromUser(string userNo)
        {
            List<PW_DepartmentDutyUserRelation> results = (from temp1 in PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo).Distinct()
                                           join temp2 in PWDB.PW_Duty on temp1.dutyNo equals temp2.dutyNo
                                           join temp3 in PWDB.PW_Duty on temp2.parentDuty equals temp3.dutyNo
                                           join temp4 in PWDB.PW_DepartmentDutyUserRelation on temp3.dutyNo equals temp4.dutyNo
                                           select (temp4)).Distinct().ToList();
            return results;
        }
    }
}